package site.xuxingcan.NettyUtil;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import site.xuxingcan.Model.BaseModel;
import site.xuxingcan.Model.LoginModel;
import site.xuxingcan.Model.RoomActionModel;

import java.util.List;

/**
 * Created by xuxingcan on 16-7-25.
 */
public class JsonDecoder extends MessageToMessageDecoder<String> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, String s, List<Object> list) throws Exception {
        BaseModel baseModel = NettyUtil.gson.fromJson(s, BaseModel.class);
        switch (baseModel.getModelType()) {
            case BaseModel.TYPE_LOGIN_MODEL:
                list.add(NettyUtil.gson.fromJson(s, LoginModel.class));
                break;
            case BaseModel.TYPE_ROOM_ACTION_MODEL:
                list.add(NettyUtil.gson.fromJson(s, RoomActionModel.class));
                break;
        }
    }
}
